import { apiProxy } from "@/utils/request"
export default {
    state: {
        userToken:localStorage.getItem('userToken'),
        userInfo:JSON.parse(localStorage.getItem('userInfo')),
    },
    getters: {
        getUserInfo(state){
            return state.userInfo    
        }
    },
    mutations: {
        login(state,res){
            state.userToken=res.token
            state.userInfo=res.user//对象
            //持久化处理
            localStorage.setItem("userToken",res.token)
            localStorage.setItem("userInfo",JSON.stringify(res.user))
        },
    },
    actions: {
        loginAsync({commit},data) {     
            return new Promise((resolve) => {
                apiProxy({
                    url: '/api/login',
                    method:'post',
                    data
                }).then(res=>{
                    commit('login',res)
                    resolve(res)
                })
            })
        }
    },
    modules: {
    }
}